using System;
using System.Linq.Expressions;
using HzyEF.Base;

namespace HzyEF.Core.CodeAnalysis
{
    /// <summary>
    /// where 解析器
    /// </summary>
    public class WhereAnalysis : ExpressionVisitor//: WhereBase
    {
        // public WhereAnalysis(SqlStorage sqlStorage) : base(sqlStorage)
        // {
        // }

        public override Expression Visit(Expression node)
        {
            return base.Visit(node);
        }

        protected override Expression VisitBinary(BinaryExpression node)
        {
            this.Visit(node.Left);
            this.Visit(node.Right);
            // return base.Visit(node);
            return node;
        }

        protected override Expression VisitMember(MemberExpression node)
        {
            return base.Visit(node);
        }

        protected override Expression VisitConstant(ConstantExpression node)
        {
            return base.Visit(node);
        }

        protected override Expression VisitUnary(UnaryExpression node)
        {
            return base.Visit(node.Operand);
        }

        protected override Expression VisitNewArray(NewArrayExpression node)
        {
            return base.Visit(node);
        }



    }

    /// <summary>
    /// where 基类
    /// </summary>
    public class WhereBase : ExpressionVisitor
    {


        public object Eval(Expression expression)
        {
            UnaryExpression cast = Expression.Convert(expression, typeof(object));
            var obj = Expression.Lambda<Func<object>>(cast).Compile().Invoke();
            // return obj != null ? GetValueFormat(obj) : obj;
            return obj;
        }





    }
}